import { Boot } from '@wangeditor/editor'
import EditorPlugin from '@/components/Editor/module/plugin/plugin.js'
import VodParseElemHtml from '@/components/Editor/module/plugin/vod/parse-elem-html.js'
import VodElemToHtml from '@/components/Editor/module/plugin/vod/elem-to-html.js'
import VodRenderElem from '@/components/Editor/module/plugin/vod/render-elem.js'
import FormulaParseElemHtml from '@/components/Editor/module/plugin/formula/parse-elem-html.js'
import FormulaElemToHtml from '@/components/Editor/module/plugin/formula/elem-to-html.js'
import FormulaRenderElem from '@/components/Editor/module/plugin/formula/render-elem.js'

// 注册菜单
export const registerMenu = function (editor, toolbarConfig, menusList) {
  const allRegisterMenu = editor.getAllMenuKeys() // 获取所有已注册的菜单
  let keys = []
  for (let item of menusList) {
    if (allRegisterMenu.indexOf(item.key) < 0) {
      const menuObj = {
        key: item.key,
        factory() {
          return new item.class()
        }
      }
      Boot.registerMenu(menuObj)
    }
    keys.push(item.key)
  }
  toolbarConfig.insertKeys = {
    index: menusList[0].index,
    keys: keys
  }
}

const module = {
  editorPlugin: EditorPlugin,
  parseElemsHtml: [VodParseElemHtml, FormulaParseElemHtml],
  elemsToHtml: [VodElemToHtml, FormulaElemToHtml],
  renderElems: [VodRenderElem, FormulaRenderElem]
}
// 注册自定义插件
Boot.registerModule(module)
